import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getMyUserInfoService } from '@/api/user'

export const useUserStore = defineStore(
  'big-user',
  () => {
    const token = ref('')
    const setToken = (v) => {
      token.value = v
    }
    const removeToken = () => {
      setToken('')
    }

    const userInfo = ref({})
    const getUserInfo = async () => {
      const res = await getMyUserInfoService()
      userInfo.value = res.data
    }
    const setUserInfo = (o) => {
      userInfo.value = o
    }
    const clearLoginData = () => {
      setUserInfo({})
      removeToken()
    }
    return {
      token,
      setToken,
      removeToken,
      userInfo,
      getUserInfo,
      setUserInfo,
      clearLoginData
    }
  },
  {
    persist: true // 持久化
  }
)
